package com.mti.epivisite.common.event
{
	import com.mti.epivisite.common.Configuration;
	
	import flash.utils.ByteArray;
	
	import media.MediaPicture;
	import media.MediaPoint;
	import media.MediaText;
	
	import mx.collections.ArrayCollection;
	import mx.messaging.Channel;
	import mx.messaging.ChannelSet;
	import mx.messaging.channels.AMFChannel;
	import mx.rpc.events.FaultEvent;
	import mx.rpc.events.ResultEvent;
	import mx.rpc.remoting.RemoteObject;
	
	public class EventProxy extends EventProxyBase
	{
		public function EventProxy()
		{
			super();
			
			var channelSet:ChannelSet = new ChannelSet();
			var channel:Channel = new AMFChannel("my-amf", Configuration.SERVER_CHANNEL_URI);
			channelSet.addChannel(channel);
			
			_remoteObject = new RemoteObject("interface");
			_remoteObject.source = "com.serverplic.media.MediaInterface";
			_remoteObject.channelSet = channelSet;
		}
		
		private var _remoteObject:RemoteObject = null;
		
		override public function load(name:String):void
		{
			_remoteObject.getEvent.addEventListener(ResultEvent.RESULT, onGetEventResult);
			_remoteObject.getEvent.addEventListener(FaultEvent.FAULT, onGetEventFault);
			
			_remoteObject.getEvent(name);
		}
		
		private function onGetEventResult(event:ResultEvent):void
		{
			_remoteObject.getEvent.removeEventListener(ResultEvent.RESULT, onGetEventResult);
			_remoteObject.getEvent.removeEventListener(FaultEvent.FAULT, onGetEventFault);
			
			if (event.result == null)
			{
				sendNotification(LOAD_ERROR, "L'évènement n'existe pas.");
			}
			else
			{
				var e:EpivisiteEvent = new EpivisiteEvent();
				e.description = event.result["description"];
				e.endDate = event.result["endDate"];
				e.id = event.result["id"];
				e.mapArray = event.result["mapArray"];
				e.mapPath = event.result["mapPath"];
				e.name = event.result["name"];
				e.password = event.result["password"];
				e.pointList = event.result["pointList"];
				e.startDate = event.result["startDate"];
				e.title = event.result["title"];
				
				baseEvent = e;
				modifiedEvent = e.Copy();
				isLoaded = true;
				sendNotification(LOAD_SUCCESS);
			}
		}
		
		private function onGetEventFault(event:FaultEvent):void
		{
			_remoteObject.getEvent.removeEventListener(ResultEvent.RESULT, onGetEventResult);
			_remoteObject.getEvent.removeEventListener(FaultEvent.FAULT, onGetEventFault);
			
			sendNotification(LOAD_ERROR, "Erreur serveur");
		}

		/* Save 
		** Enregistre l'event donné dans la base.
		** Envoie une notification SAVE_SUCCESS si l'opération a réussi
		** Envoie une notification SAVE_ERROR si l'opération a échouée (event existe déjà ou serveur planté)
		*/
		override public function save(event:EpivisiteEvent):void
		{
			_remoteObject.getEvent.addEventListener(ResultEvent.RESULT, onSaveEventResult);
			_remoteObject.getEvent.addEventListener(FaultEvent.FAULT, onSaveEventFault);
			baseEvent = event;
			_remoteObject.getEvent(event.name);
		}
		
		private function onSaveEventResult(event:ResultEvent):void
		{
			_remoteObject.getEvent.removeEventListener(ResultEvent.RESULT, onSaveEventResult);
			_remoteObject.getEvent.removeEventListener(FaultEvent.FAULT, onSaveEventFault);
			
			if (event.result != null)
			{
				sendNotification(SAVE_ERROR, "L'évènement existe déjà.");
			}
			else
			{
				_remoteObject.createEvent(baseEvent.name, baseEvent.title, baseEvent.password, baseEvent.description, baseEvent.mapArray, baseEvent.startDate, baseEvent.endDate);
				modifiedEvent = baseEvent.Copy();
				sendNotification(SAVE_SUCCESS);
			}
			
			isLoaded = true;
		}
		
		private function onSaveEventFault(event:FaultEvent):void
		{
			_remoteObject.getEvent.removeEventListener(ResultEvent.RESULT, onSaveEventResult);
			_remoteObject.getEvent.removeEventListener(FaultEvent.FAULT, onSaveEventFault);
			
			sendNotification(SAVE_ERROR, "Erreur serveur");
		}
		
		/* UpdateX
		** Modifie l'event donné dans la base.
		*/
		override public function updatePassword(event:EpivisiteEvent, pass:String):void
		{
			_remoteObject.updateEventPassword(event.name, event.password, pass);
		}
		override public function updateDescription(event:EpivisiteEvent, desc:String):void
		{
			_remoteObject.updateEventDescription(event.name, event.password, desc);
		}
		override public function updateStartDate(event:EpivisiteEvent, date:Date):void
		{
			_remoteObject.updateEventStartDate(event.name, event.password, date);
		}
		override public function updateEndDate(event:EpivisiteEvent, date:Date):void
		{
			_remoteObject.updateEventEndDate(event.name, event.password, date);
		}
		override public function updateMap(event:EpivisiteEvent, array:ByteArray):void
		{
			_remoteObject.updateEventMap(event.name, event.password, array);
		}
		override public function updateTitle(event:EpivisiteEvent, title:String):void
		{
			_remoteObject.updateEventMap(event.name, event.password, title);
		}
		override public function deleteEvent(event:EpivisiteEvent):void
		{
			_remoteObject.deleteEvent(event.name, event.password);
		}
		override public function updatePointDescription(event:EpivisiteEvent, point:MediaPoint):void
		{
			_remoteObject.updatePointDescription(point.name, event.name, event.password, point.description);
		}
		override public function updatePointPositionX(event:EpivisiteEvent, point:MediaPoint):void
		{
			_remoteObject.updatePointPositionX(point.name, event.name, event.password, point.posX);
		}
		override public function updatePointPositionY(event:EpivisiteEvent, point:MediaPoint):void
		{
			_remoteObject.updatePointPositionY(point.name, event.name, event.password, point.posY);
		}
		
		
		/* getPoints 
		** Récupère la liste des points d'un event.
		** Envoie une notification GET_POINTS_SUCCESS si l'opération a réussi
		** Envoie une notification GET_POINTS_ERROR si l'opération a échouée
		** Enregistre l'event donné en paramètre dans "data" en y ajoutant la liste de waypoint
		*/
		override public function getPoints(event:EpivisiteEvent):void
		{
			_remoteObject.getPointList.addEventListener(ResultEvent.RESULT, onGetPointsEventResult);
			_remoteObject.getPointList.addEventListener(FaultEvent.FAULT, onGetPointsEventFault);
			_remoteObject.getPointList(event.name);
		}
		
		private function onGetPointsEventResult(event:ResultEvent):void
		{
			_remoteObject.getEvent.removeEventListener(ResultEvent.RESULT, onGetPointsEventResult);
			_remoteObject.getEvent.removeEventListener(FaultEvent.FAULT, onGetPointsEventFault);
			
			if (event.result != null)
			{
				var pointList:ArrayCollection = event.result as ArrayCollection;
				baseEvent.pointList = pointList;
				modifiedEvent.pointList = pointList;
			}
			
			isLoaded = true;
			sendNotification(GET_POINTS_SUCCESS);
		}
		
		private function onGetPointsEventFault(event:FaultEvent):void
		{
			_remoteObject.getEvent.removeEventListener(ResultEvent.RESULT, onGetPointsEventResult);
			_remoteObject.getEvent.removeEventListener(FaultEvent.FAULT, onGetPointsEventFault);
			
			sendNotification(GET_POINTS_ERROR);
		}
		override public function addPoint(mediapoint:MediaPoint, event:EpivisiteEvent):void
		{
			_remoteObject.createPoint(mediapoint.name, event.name, event.password, mediapoint.title, mediapoint.QRCode, mediapoint.shortDescription, mediapoint.description, mediapoint.posX, mediapoint.posY, 1);
		}
		override public function deletePoint(event:EpivisiteEvent, name:String):void
		{
			_remoteObject.deletePoint(name, event.name, event.password);
		}
		override public function addPicture(event:EpivisiteEvent, picture:MediaPicture, array:ByteArray):void
		{
			_remoteObject.createPicture(picture.name, picture.point.name, event.name, event.password, picture.title, picture.description, array);	
		}
		override public function deletePicture(event:EpivisiteEvent, picturename:String, pointname:String):void
		{
			_remoteObject.deletePicture(picturename, pointname, event.name, event.password);
		}
		override public function updatePictureDescription(event:EpivisiteEvent, picture:MediaPicture):void
		{
			_remoteObject.updatePictureDescription(picture.name, picture.point.name, event.name, event.password, picture.description);	
		}
		override public function updatePictureImage(event:EpivisiteEvent, picture:MediaPicture, array:ByteArray):void
		{
			_remoteObject.updatePictureImage(picture.name, picture.point.name, event.name, event.password, array);
		}
		override public function addText(event:EpivisiteEvent, text:MediaText):void
		{
			_remoteObject.createText(text.name, text.point.name, event.name, event.password, text.title, text.shortText, text.text);	
		}
		override public function updateTextShortText(event:EpivisiteEvent, text:MediaText):void
		{
			_remoteObject.updatePictureDescription(text.name, text.point.name, event.name, event.password, text.shortText);	
		}
		override public function updateTextText(event:EpivisiteEvent, text:MediaText):void
		{
			_remoteObject.updatePictureDescription(text.name, text.point.name, event.name, event.password, text.text);	
		}
		override public function deleteText(event:EpivisiteEvent, textname:String, pointname:String):void
		{
			_remoteObject.deleteText(textname, pointname, event.name, event.password);
		}
	}
}